import org.apache.commons.codec.digest.DigestUtils as DU

class BootStrap {

    def init = {servletContext ->
        def userRole = new Role(authority: "ROLE_USER", description: "User").save()
        def adminRole = new Role(authority: "ROLE_ADMIN", description: "Administrator").save()

        assert userRole != null
        assert adminRole != null

        def stdUser = new User(username: "user", passwordHash: DU.md5Hex("user01"), authorities: [userRole], enabled: true, userRealName: "User", email: "user@user.com", emailShow: true)
        stdUser.save()

        def adminUser = new User(username: "admin", passwordHash: DU.md5Hex("admin01"), authorities: [adminRole], enabled: true, userRealName: "Administrator", email: "admin@admin.com", emailShow: true)
        adminUser.authorities.add(adminRole)
        adminUser.save()

        assert stdUser != null
        assert adminUser != null


        def bug = new IssueType(code: "B", name: "Bug").save()
        def newFeature = new IssueType(code: "F", name: "New Feature").save()

        assert bug != null
        assert newFeature != null

        def open = IssueStatus.OPEN.save()
        def inProgress = IssueStatus.IN_PROGRESS.save()
        def resolved = IssueStatus.RESOLVED.save()
        def closed = IssueStatus.CLOSED.save()

        assert open != null
        assert inProgress != null
        assert resolved != null
        assert closed != null


        def trivial = new IssueSeverity(code: "T", name: "Trivial").save()
        def blocker = new IssueSeverity(code: "B", name: "Blocker").save()

        assert trivial != null
        assert blocker != null

        def fixed = new IssueResolution(code: "F", name: "Fixed").save()
        def wontFix = new IssueResolution(code: "W", name: "WontFix").save()

        assert fixed != null
        assert wontFix != null

        def project = new Project(code: "BSPP", name: "Bug System Plus Plus")
        project.save()

        //  def issue1 = new Issue(number: 1, project: project).save()

        //  def issue2 = new Issue(number:2, project: project).save()


        def projectMembership = new ProjectMembership(user: stdUser, project: project).save()

        assert project != null
        //  assert issue1 != null
        //  assert issue2 != null
        assert projectMembership != null
    }

    def destroy = {
    }
}
